package http

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ApiResponse struct {
	Code       int         `json:"code"`                 // HTTP status code
	Message    string      `json:"message"`              // Human-readable message
	Data       interface{} `json:"data"`                 // Response payload
	Pagination *Pagination `json:"pagination,omitempty"` // Optional pagination
}

type Pagination struct {
	Page      int `json:"page"`       // Current page number
	PageSize  int `json:"page_size"`  // Number of items per page
	Total     int `json:"total"`      // Total number of items
	TotalPage int `json:"total_page"` // Total number of pages
}

func Success(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, ApiResponse{
		Code:    http.StatusOK,
		Message: "success",
		Data:    data,
	})
}

func Error(c *gin.Context, code int, message string) {
	c.JSON(code, ApiResponse{
		Code:    code,
		Message: message,
		Data:    nil,
	})
}

func Paginated(c *gin.Context, data interface{}, pagination Pagination) {
	c.JSON(http.StatusOK, ApiResponse{
		Code:       http.StatusOK,
		Message:    "success",
		Data:       data,
		Pagination: &pagination,
	})
}

func ValidationError(c *gin.Context, err error) {
	Error(c, http.StatusBadRequest, fmt.Sprintf("Validation error: %v", err))
}
